'use strict';

/**
 * @ngdoc directive
 * @name jgappApp.directive:deleteFlow
 * @description
 * # deleteFlow
 */
angular.module('jgappApp')
    .directive('deleteFlow', ['Util', '$location', '$rootScope', 'DataService', 'openApi', function (Util, $location, $rootScope, DataService, openApi) {
        return {
            scope: {
                uid: "=uid",
                submit: "=submit",
                validate: "=validate"
            },
            template: '<div class="flex footer tc flow-btn">' +
                '<div class="w50" ng-click="save(-1)" ng-if="showDraft||!uid"><span class="save-draft"></span>保存草稿</div>' +
                '<div class="w50 c-blue" ng-click="save(-2)"><span class="submit-flow"></span>提交</div>' +
                '<div class="w50" ng-click="showconfirm()" ng-if="showUpdate"><span class="delete-flow"></span>删除</div>' +
                '</div>',
            restrict: 'AE',
            replace: true,
            link: function postLink(scope, element, attrs) {
                // 提交按钮一直有
                // 如果有流程类型说明是驳回则显示删除
                // 没有流程类型说明新建流程或者草稿，显示草稿按钮
                scope.TVtype = Util.getTVType();
                switch (scope.TVtype) {
                    case Util.DRAFT: //草稿
                        scope.showDraft = true
                        break;
                    case Util.UPDATE: //驳回
                        scope.showUpdate = true
                        break;
                    default:
                }
                // 删除流程
                scope.showconfirm = function () {
                    navigator.notification.confirm(
                        '是否删除该流程？',
                        confirm,
                        '删除流程', ['删除', '取消']
                    )
                };
                var confirm = function (btn) {
                    var param = {};
                    param.processInstanceId = attrs.id
                    param.deleteReason = scope.auditText
                    DataService.getData(openApi.deleteprocess, param).then(function (data) {
                        Util.Toast(data, {
                            callback: function () {
                                NativeUtil.use("close");
                            },
                            time: 1000
                        })
                        NativeUtil.use("updateProcess");
                    });
                };
                scope.save = function (type) {
                    var check = scope.validate();
                    if (check) {
                        scope.submit(type);
                    }
                }
            }
        };
    }]);
